package com.mogujie.tt.DB.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.mogujie.tt.DB.entity.AddFriendNotifyEntity;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by user on 2015/10/23.
 */
public class AddFriendNotifyDao  extends AbstractDao<AddFriendNotifyEntity,Long> {

    public static final String TABLENAME = "AddFriendNotifyInfo";

    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property PeerId = new Property(1, int.class, "peerId", false, "PEER_ID");
        public final static Property MainName = new Property(2, String.class, "mainName", false, "MAIN_NAME");
        public final static Property Avatar = new Property(3, String.class, "avatar", false, "AVATAR");
        public final static Property Relation = new Property(4, int.class, "relation", false, "RELATION");
        public final static Property AuthInformation = new Property(5, String.class, "authInformation", false,"AUTH_INFORMATION");
    };

    public AddFriendNotifyDao(DaoConfig config) {super(config);}

    public AddFriendNotifyDao(DaoConfig config, AbstractDaoSession daoSession) {super(config, daoSession);}

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'AddFriendNotifyInfo' (" + //
                "'_id' INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "'PEER_ID' INTEGER NOT NULL UNIQUE ," + // 1: peerId
                "'MAIN_NAME' TEXT NOT NULL ," + // 2: mainName
                "'AVATAR' TEXT NOT NULL ," + // 3: avatar
                "'RELATION' INTEGER NOT NULL ," + //4:Relation
                "'AUTH_INFORMATION' TEXT NOT NULL );"); //5:AuthInformation
        // Add Indexes
        db.execSQL("CREATE INDEX " + constraint + "IDX_AddFriendNotifyInfo_PEER_ID ON AddFriendNotifyInfo" +
                " (PEER_ID);");
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'AddFriendNotifyInfo'";
        db.execSQL(sql);
    }


    @Override
    protected void bindValues(SQLiteStatement stmt, AddFriendNotifyEntity entity) {
        stmt.clearBindings();
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getPeerId());
        stmt.bindString(3, entity.getMainName());
        stmt.bindString(4, entity.getAvatar());
        stmt.bindLong(5, entity.getRelation());
        stmt.bindString(6, entity.getAuthInformation());
    }

    @Override
    protected AddFriendNotifyEntity readEntity(Cursor cursor, int offset) {
        AddFriendNotifyEntity entity = new AddFriendNotifyEntity( //
                cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
                cursor.getInt(offset + 1), // peerId
                cursor.getString(offset + 2), // mainName
                cursor.getString(offset + 3), // avatar
                cursor.getInt(offset + 4), //relation
                cursor.getString(offset + 5)  //AuthInformation
        );
        return entity;
    }

    @Override
    protected Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    @Override
    protected void readEntity(Cursor cursor, AddFriendNotifyEntity entity, int offset) {

        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setPeerId(cursor.getInt(offset + 1));
        entity.setMainName(cursor.getString(offset + 2));
        entity.setAvatar(cursor.getString(offset + 3));
        entity.setRelation(cursor.getInt(offset + 4));
        entity.setAuthInformation(cursor.getString(offset + 5));

    }

    @Override
    protected Long updateKeyAfterInsert(AddFriendNotifyEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }

    @Override
    protected Long getKey(AddFriendNotifyEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }
}
